package com.company;

public class LinkedList<E> {
    /**
     * 掺入数据 搜索数据  删除数据 用户不需要理解
     * 循环队列 双端队列
     */
    private class Node {
        public E e;
        public Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }

    private Node dummyhead;
    private int size;

    public LinkedList() {
        dummyhead = new Node(null, null);
        size = 0;
    }

    //获取链表中元素的个数
    public int getSize() {
        return size;
    }

    //返回链表是否为空
    public boolean isEmpty() {
        return size == 0;
    }

    //在链表头部添加元素
    public void addFirst(E e) {
        add(0, e);
    }
    //在 链表的index=(0-based)位置添加新的元素
    //练习使用 在实际废除

    public void add(int index, E e) {
        if (index < 0 || index > size)
            throw new IllegalArgumentException("Add failed.Igiile index.");
        Node prev = dummyhead;
        for (int i = 0; i < index; i++)
            prev = prev.next;
        prev.next = new Node(e, prev.next);
        size++;
    }

    //在尾部添加元素
    public void addLast(E e) {
        add(size, e);
    }

    //查询
    public E get(int index) {
        if (index < 0 || index > size)
            throw new IllegalArgumentException("Get failed.Igiile index.");
        Node cur = dummyhead.next;//索引0开始
        for (int i = 0; i < index; i++)
            cur = cur.next;
        return cur.e;
    }

    public E getFirst() {
        return get(0);
    }

    public E getLast() {
        return get(size - 1);
    }

    public void set(int index, E e) {
        if (index < 0 || index > size)
            throw new IllegalArgumentException("Set failed.Igiile index.");
        Node cur = dummyhead.next;//索引0开始
        for (int i = 0; i < index; i++)
            cur = cur.next;
        cur.e = e;
    }

    //查找链表中的是否有元素
    public boolean contains(E e) {
        Node cur = dummyhead.next;
        while (cur != null) {
            if (cur.e == e)
                return true;
            cur = cur.next;
        }
        return false;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        Node cur = dummyhead.next;
        while (cur != null) {
            res.append(cur + "->");
            cur = cur.next;
        }
        res.append("NULL");
        return res.toString();
    }

    //从链表中index 0-based 删除元素
    public E reove(int index) {
        if (index < 0 || index > size)
            throw new IllegalArgumentException("Set failed.Igiile index.");
        Node prev = dummyhead;//索引0开始
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        Node retNode = prev.next;
        prev.next = retNode.next;
        retNode.next = null;
        size--;
        return retNode.e;
    }
    public E removeLast(){
        return reove(size-1);
    }
    public E removeFirst(){
        return  reove(0);
    }

    public static void main(String[] args) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        for (int i = 0; i < 5; i++) {
            linkedList.addFirst(i);
            System.out.println(linkedList);
        }
        linkedList.add(2, 666);
        System.out.println(linkedList);
        linkedList.reove(2);
        System.out.println(linkedList);
        linkedList.removeFirst();
        System.out.println(linkedList);
        linkedList.removeLast();
        System.out.println(linkedList);
    }
    //pre.next=delnode.next
    //delnode.next=null
    // addFirt o1
    // add 0n
    // addLast on
    //remove on
    // remove last on
    // remove head
    // 01
    //数组索引数据结构
    //其实链表 不去修改 增加 删除 对数据头处理 动态的查找
    //改进方式
    // o1
    //最基础的动态数据结构 二叉树 红黑树 AVL
    //都在栈顶操作 链表作为栈的定测部分社会
}
